package com.example.paper.testSetList;

import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.time.Instant;
import java.util.*;

/**
 * set list 性能对比测试用例
 */
@Slf4j
public class SetTest {

    public static void main(String[] args) {
        // 字符串测试 生成长度为30的随机字符串10000个，进行10000次contains操作
        List<String> list = RandomStringGenerator.generateRandomStringList(30, 10000);
        shortStrTest(list, 1);
        shortStrTest(list, 2);
        shortStrTest(list, 3);
    }
    private static void shortStrTest(List<String> strList, Integer count) {
        String testStr = "T5r3eU33iOvbb";

        testTime(strList, testStr, count);
    }
    private static void testTime(List<String> list, String testStr, Integer count) {
        Instant begin1 = Instant.now();
        for (int i = 0; i < 10000; i++) { list.contains(testStr); }
        Instant end1 = Instant.now();
        log.info("字符串List第{}次耗时:{}", count, Duration.between(begin1, end1).toMillis());
        Set<String> set = new HashSet<>(list);
        Instant begin2 = Instant.now();
        for (int i = 0; i < 10000; i++) { set.contains(testStr); }
        Instant end2 = Instant.now();
        log.info("字符串Set第{}次耗时:{}", count, Duration.between(begin2, end2).toMillis());
    }
}
